<div class="buttons" style="display:none;">
  <div class="pull-right">
    <input type="button" value="{{ button_confirm }}" id="button-confirm" data-loading-text="{{ text_loading }}" class="btn btn-primary" />
  </div>
</div>
<div class="flaot-pay hidden-lg hidden-md hidden-sm">
  <div class="price">{{ total }}</div>
  <input type="button" value="{{ button_confirm }}" id="button-confirm-mobile" data-loading-text="{{ text_loading }}" class="blue-btn-confirm" />
</div>
<script>
    $('#button-confirm-mobile').on('click',function () {
        $('#button-confirm').click();
    })
</script>
<script type="text/javascript"><!--
    {% if not logged %}
    $('#button-confirm').on('click', function() {
        $.ajax({
            url: 'index.php?route=checkout/guest/savehebing',
            type: 'post',
            data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select,#lessmessagesave textarea'),
            dataType: 'json',
            beforeSend: function() {
                $("#button-confirm-mobile").attr('disabled',true);

                $("#button-confirm").attr('disabled',true);
            },
            complete: function() {
                //$("#button-confirm-mobile").attr('disabled',false);
                //$("#button-confirm").attr('disabled',false);
            },
            success: function(json) {

                if (json['redirect']) {
                    location = json['redirect'];
                } else if (json['error']) {
                    $('.form-group').removeClass('has-error');
                    var elementnoteo='';
                    for (var i in json['error']) {
                        var element = $(('#input-payment-' + i).replace('_', '-'));
                        if(!elementnoteo){
                            elementnoteo=element;
                            $('.text-danger').remove();
                            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
                            // window.location.hash = "#"+elementnoteo.attr("id");
                            $(window).scrollTop(element.offset().top-75);
                        }

                    }
                    $("#button-confirm-mobile").attr('disabled',false);
                    $("#button-confirm").attr('disabled',false);
                    //console.log(element.attr("id"));

                    $('.text-danger').parent().addClass('has-error');
                } else {

                    var shoppingsameaddress = $('#collapse-payment-address input[name=\'shoppingsameaddress\']:checked').val();
                    if(shoppingsameaddress=='shoppingdifferent'){
                        console.log(shoppingsameaddress);
                        savegshoping();
                    }else{
                        savegpaddess();
                    }


                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });

    });
    {% else %}
    $('#button-confirm').on('click', function() {
        $.ajax({
            url: 'index.php?route=checkout/payment_address/savehebingboth',
            type: 'post',
            data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select,#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select,#lessmessagesave textarea'),
            dataType: 'json',
            beforeSend: function() {
                $("#button-confirm-mobile").attr('disabled',true);
                $("#button-confirm").attr('disabled',true);
            },
            complete: function() {
                // $("#button-confirm-mobile").attr('disabled',false);
                //$("#button-confirm").attr('disabled',false);
            },
            async:false,
            success: function(json) {
                $('.alert-dismissible, .text-danger').remove();
                $('.form-group').removeClass('has-error');

                if (json['redirect']) {
                    location = json['redirect'];
                } else if (json['error']) {
                    if (json['error']['warning']) {
                        $('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                    }
                    var elementnoteo='';
                    for (i in json['error']) {
                        var element = $('#input-payment-' + i.replace('_', '-'));
                        if(!elementnoteo){
                            elementnoteo=element;

                            if (element.parent().hasClass('input-group')) {
                                if ($(element).parent().children().hasClass('text-danger')) {
                                    $('.text-danger').remove();
                                }
                                $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
                                window.location.hash = "#"+elementnoteo.attr("id");
                            } else {
                                if ($(element).parent().children().hasClass('text-danger')) {
                                    $('.text-danger').remove();
                                }
                                $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
                                window.location.hash = "#"+elementnoteo.attr("id");
                            }
                        }
                        //if ($(element).parent().hasClass('input-group')) {
                        //$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
                        //} else {
                        //$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
                        //}
                    }
                    //window.location.hash = "#"+elementnoteo.attr("id");

                    var elementnotes='';
                    for (i in json['error']) {

                        var element = $('#input-shipping-' + i.replace('_', '-'));
                        if(!elementnotes){
                            elementnotes=element;
                            if ($(element).parent().hasClass('input-group')) {
                                if ($(element).parent().children().hasClass('text-danger')) {
                                    $('.text-danger').remove();
                                }
                                $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
                            } else {
                                if ($(element).parent().children().hasClass('text-danger')) {
                                    $('.text-danger').remove();
                                }
                                $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
                            }
                            window.location.hash = "#"+elementnotes.attr("id");
                        }

                    }
                    $("#button-confirm-mobile").attr('disabled',false);
                    $("#button-confirm").attr('disabled',false);
                    // Highlight any found errors
                    $('.text-danger').parent().parent().addClass('has-error');
                } else {

                    ///////
                    $.ajax({
                        url: 'index.php?route=checkout/confirm_he',
                        dataType: 'html',

                        success: function(html) {
                            //$('#collapse-checkout-confirm .panel-body').html(html);

                            $.ajax({
                                url: 'index.php?route=extension/payment/cod/confirm',
                                dataType: 'json',
                                beforeSend: function() {
                                    $('#button-confirm').button('loading');
                                },
                                complete: function() {
                                    $('#button-confirm').button('reset');
                                },
                                success: function(json) {
                                    if (json['redirect']) {
                                        location = json['redirect'];
                                    }
                                },
                                error: function(xhr, ajaxOptions, thrownError) {
                                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                }
                            });

                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }
                    });
                    ///////


                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });

    });
    {% endif %}

    function savegpaddess(){
        var shipping_address = $('#collapse-payment-address input[name=\'shipping_address\']:checked').prop('value');

        if (!shipping_address) {
            $.ajax({
                url: 'index.php?route=checkout/shipping_method_hebing',
                dataType: 'html',
                beforeSend: function() {
                    $("#button-confirm-mobile").attr('disabled',true);
                    $("#button-confirm").attr('disabled',true);
                },
                complete: function() {
                    //$("#button-confirm-mobile").attr('disabled',false);
                    //$("#button-confirm").attr('disabled',false);
                },
                success: function(html) {
                    // Add the shipping address
                    $.ajax({
                        url: 'index.php?route=checkout/payment_method_hebing',
                        dataType: 'html',
                        complete: function() {
                            ///$('#button-guest').button('reset');
                        },
                        success: function(html) {
                            $('#collapse-payment-method .panel-body').html(html);
                            ///////
                            $.ajax({
                                url: 'index.php?route=checkout/confirm_he',
                                dataType: 'html',

                                success: function(html) {
                                    //$('#collapse-checkout-confirm .panel-body').html(html);

                                    $.ajax({
                                        url: 'index.php?route=extension/payment/cod/confirm',
                                        dataType: 'json',
                                        beforeSend: function() {
                                            $('#button-confirm').button('loading');
                                        },
                                        complete: function() {
                                            $('#button-confirm').button('reset');
                                        },
                                        success: function(json) {
                                            if (json['redirect']) {
                                                location = json['redirect'];
                                            }
                                        },
                                        error: function(xhr, ajaxOptions, thrownError) {
                                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                        }
                                    });

                                },
                                error: function(xhr, ajaxOptions, thrownError) {
                                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                }
                            });
                            ///////



                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }
                    });

                    //$('#collapse-shipping-method .panel-body').html(html);


                    //$('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('{{ text_checkout_payment_method }}');
                    //$('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('{{ text_checkout_confirm }}');
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    $("#button-confirm-mobile").attr('disabled',false);
                    $("#button-confirm").attr('disabled',false);
                }
            });
        }
    }
    function savegshoping(){
        $.ajax({
            url: 'index.php?route=checkout/guest_shipping/savehebingdif',
            type: 'post',
            data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
            dataType: 'json',
            beforeSend: function() {
                $("#button-confirm-mobile").attr('disabled',true);
                $("#button-confirm").attr('disabled',true);
            },
            complete: function() {
                //$("#button-confirm-mobile").attr('disabled',false);
                // $("#button-confirm").attr('disabled',false);
            },
            success: function(json) {
                $('.alert-dismissible, .text-danger').remove();
                $('.form-group').removeClass('has-error');

                if (json['redirect']) {
                    location = json['redirect'];
                } else if (json['error']) {
                    $('#button-guest-shipping').button('reset');

                    if (json['error']['warning']) {
                        // $('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-danger alert-dismissible">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                    }
                    var elementnote='';
                    for (i in json['error']) {
                        var element = $('#input-shipping-' + i.replace('_', '-'));
                        if(!elementnote){

                            elementnote=element;

                        }
                        if ($(element).parent().hasClass('input-group')) {
                            if ($(element).parent().children().hasClass('text-danger')) {
                                $('.text-danger').remove();
                            }
                            $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
                        } else {
                            if ($(element).parent().children().hasClass('text-danger')) {
                                $('.text-danger').remove();
                            }
                            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
                        }
                        window.location.hash = "#"+elementnote.attr("id");
                    }

                    $("#button-confirm-mobile").attr('disabled',false);
                    $("#button-confirm").attr('disabled',false);

                    // Highlight any found errors
                    $('.text-danger').parent().addClass('has-error');
                } else {
                    savegpaddess();

                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });


    }
    //--></script>

